﻿
/// <summary>
// DBLinq.MySqlProviders - .NET Providers for MySql using the framework DBLinq under MIT license, author Jiri George Moudry
// http://linq.to/db
// and the CodeFx.MySqlProviders under GNU licence, author Laurent Morisseau
// http://www.codeplex.com/CodeFxMySqlProvider
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Laurent Morisseau
/// </summary>
/// <author><a href="mailto:laurent.morisseau@gmail.com">Laurent Morisseau</a></author>
/// 
using DBLinq.MySqlProviders;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MySql.Data.MySqlClient;
using System;
namespace DBLinq.MySqlProviders.TestProject
{
    
    
    /// <summary>
    ///This is a test class for AspNetUsersInRolesDTOTest and is intended
    ///to contain all AspNetUsersInRolesDTOTest Unit Tests
    ///</summary>
    [TestClass()]
    public class AspNetUsersInRolesDTOTest
    {
        private MySqlConnection conn;
        private uint appid = 0;
        private uint userid = 0;


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        [TestInitialize()]
        public void MyTestInitialize()
        {
            ConnectionString.Current.CurrentString = TestHelper.connectionstring;
            conn = TestHelper.Open();
            TestHelper.DeleteApplicationTest(conn);
            appid = TestHelper.InsertApplicationTest(conn);
        }

        [TestCleanup()]
        public void MyTestCleanup()
        {
            TestHelper.DeleteApplicationTest(conn);
            TestHelper.Close(conn);
        }

        #endregion


        /// <summary>
        ///A test for Insert
        ///</summary>
        [TestMethod()]
        public void InsertTest()
        {
            
            uint roleid = TestHelper.InsertRole(conn, appid);
            userid = TestHelper.InsertUserTest(conn, appid);

            AspNetUsersInRole user = new AspNetUsersInRole();
            user.RoleId= roleid ;
            user.UserId = userid ;
            AspNetUsersInRolesDTO.Insert(user);

            try
            {
                MySqlCommand cmd = new MySqlCommand("SELECT roleid FROM AspNet_Usersinroles WHERE userid=?userid limit 0,1", conn);
                cmd.Parameters.Add(new MySqlParameter("?userid", userid));
                object reader = cmd.ExecuteScalar();
                Assert.IsNotNull(reader);
                Assert.AreEqual(roleid , (uint)reader);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                TestHelper.DeleteUserinRole(conn, userid);
                TestHelper.DeleteRoleTest(conn, appid);
                TestHelper.DeleteUserTest(conn);
            }
	
        }

        /// <summary>
        ///A test for Exist
        ///</summary>
        [TestMethod()]
        public void ExistTest1()
        {
            /// Init
            uint roleid = TestHelper.InsertRole(conn, appid);
            userid = TestHelper.InsertUserTest(conn, appid);
            TestHelper.InsertUserInRole(conn, userid, roleid);

            /// Tests
            bool actual = AspNetUsersInRolesDTO.Exist(userid , roleid );
            try
            {
                Assert.AreEqual<bool>(actual, true);
            }
            finally
            {
                TestHelper.DeleteUserinRole(conn, userid);
                TestHelper.DeleteRoleTest(conn, appid);
                TestHelper.DeleteUserTest(conn);
            }
        }

        /// <summary>
        ///A test for Exist
        ///</summary>
        [TestMethod()]
        public void ExistTest()
        {
            /// Init
            uint roleid = TestHelper.InsertRole(conn, appid);
            userid = TestHelper.InsertUserTest(conn, appid);
            TestHelper.InsertUserInRole(conn, userid, roleid);

            /// Tests
            bool actual = AspNetUsersInRolesDTO.Exist(roleid);
            try
            {
                Assert.AreEqual<bool>(actual, true);
            }
            finally
            {
                TestHelper.DeleteUserinRole(conn, userid);
                TestHelper.DeleteRoleTest(conn, appid);
                TestHelper.DeleteUserTest(conn);
            }
        }

        /// <summary>
        ///A test for DeleteByFk_UserId_RoleId
        ///</summary>
        [TestMethod()]
        public void DeleteByFk_UserId_RoleIdTest()
        {
            /// Init
            uint roleid = TestHelper.InsertRole(conn, appid);
            userid = TestHelper.InsertUserTest(conn, appid);
            TestHelper.InsertUserInRole(conn, userid, roleid);

            try
            {
                AspNetUsersInRolesDTO.DeleteByFk_UserId_RoleId(userid, roleid);
                bool actual = AspNetUsersInRolesDTO.Exist(roleid, userid);
                Assert.AreEqual<bool>(actual, false);
            }
            finally
            {

                TestHelper.DeleteUserinRole(conn, userid);
                TestHelper.DeleteRoleTest(conn, appid);
                TestHelper.DeleteUserTest(conn);
            }

        }

        /// <summary>
        ///A test for DeleteByFk_UserId
        ///</summary>
        [TestMethod()]
        public void DeleteByFk_UserIdTest()
        {
            /// Init
            uint roleid = TestHelper.InsertRole(conn, appid);
            userid = TestHelper.InsertUserTest(conn, appid);
            TestHelper.InsertUserInRole(conn, userid, roleid);

            try
            {
                AspNetUsersInRolesDTO.DeleteByFk_UserId(userid);
                bool actual = AspNetUsersInRolesDTO.Exist(roleid, userid);
                Assert.AreEqual<bool>(actual, false);
            }
            finally
            {

                TestHelper.DeleteUserinRole(conn, userid);
                TestHelper.DeleteRoleTest(conn, appid);
                TestHelper.DeleteUserTest(conn);
            }
        }

        /// <summary>
        ///A test for DeleteByFk_RoleId
        ///</summary>
        [TestMethod()]
        public void DeleteByFk_RoleIdTest()
        {
            /// Init
            uint roleid = TestHelper.InsertRole(conn, appid);
            userid = TestHelper.InsertUserTest(conn, appid);
            TestHelper.InsertUserInRole(conn, userid, roleid);

            try
            {
                AspNetUsersInRolesDTO.DeleteByFk_RoleId(roleid );
                bool actual = AspNetUsersInRolesDTO.Exist(roleid, userid);
                Assert.AreEqual<bool>(actual, false);
            }
            finally
            {

                TestHelper.DeleteUserinRole(conn, userid);
                TestHelper.DeleteRoleTest(conn, appid);
                TestHelper.DeleteUserTest(conn);
            }
        }
    }
}
